package com.saikei.statstitlebar;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.List;


public class TitleBlock extends RelativeLayout implements TitleHelp {

    private static final String TAG = "StatsTileBar";

    private int barColor = Color.parseColor("#541e24");  //默认的标题栏颜色
    private final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;
    private final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;

    private static final int barViewHeight = 54;//barView的高度
    private static final int TOPVIEWID = 101;//顶部占用的状态栏ID
    private static final int BARVIEWID = 102;//barView的ID
    private static final int BARTEXTID = 103;//主标题ID
    private static final int BARSUBTEXTID = 1010;//副标题ID
    private static final int LEFTBODYID = 104;
    private static final int RIGHTBODYID = 105;

    private LinearLayout centerBody;//主标题和副标题容器
    private RelativeLayout barView; //标题栏主容器布局
    private View topView; //状态栏占用View
    private int viewHeight = 0;//标题栏的高度(barView)
    private int statusBarHeight = 0; //状态栏高度
    private final Context context;
    int leftBodyPadding;

    public TitleBlock(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initTitleUtil();
        initBarView();
    }

    private void initTitleUtil() {
        statusBarHeight = TitleUtil.getStatusBarHeight(context);
        viewHeight = DpUtil.dp2PxInt(context, barViewHeight);
        leftBodyPadding = DpUtil.dp2PxInt(context, 10);
    }

    /**
     * @Author:gewu
     * @Time:2022/10/2122:05
     * @Description:初始化标题栏布局容器
     */
    private void initBarView() {
        topView = new View(context);
        topView.setId(TOPVIEWID);
        LayoutParams viewParams = getLayoutParams(MATCH_PARENT, statusBarHeight);
        topView.setLayoutParams(viewParams);
        addView(topView);

        barView = new RelativeLayout(context);
        barView.setId(BARVIEWID);
        barView.setMinimumHeight(viewHeight);
        LayoutParams titleBarParams = getLayoutParams(MATCH_PARENT, WRAP_CONTENT);
        titleBarParams.addRule(BELOW, topView.getId());
        addView(barView, titleBarParams);
        setBackgroundColor(barColor);
    }

    private BarTextView barTextView = null; //标题的文字
    private BarTextView barSubTextView = null; //标题的文字
    private String barText = "默认标题"; //保存上次设置的
    private String subBarText = "默认副标题";

    /**
     * @Author:gewu
     * @Time:2022/10/2723:13
     * @Description:barText保存的是上一次，主标题设置的文字,防止设置副标题的时候主标题文字不显示/显示错误
     * @Description:subBarText保存的是上一次，副标题设置的文字,防止设置主标题的时候副标题标题文字不显示/显示错误
     */
    private void initBarText(String barText, String subBarText) {
        if (centerBody == null) {
            centerBody = new LinearLayout(context);
            centerBody.setOrientation(LinearLayout.VERTICAL);
            LayoutParams layoutParams = getLayoutParams(WRAP_CONTENT, WRAP_CONTENT);
            layoutParams.addRule(CENTER_IN_PARENT, barView.getId());
            centerBody.setGravity(Gravity.CENTER);
            centerBody.setLayoutParams(layoutParams);
            barView.addView(centerBody);
        }

        if (centerBody != null && barTextView == null) {
            barTextView = getBarTextView();
            barTextView.setId(BARTEXTID);
            barTextView.setGravity(Gravity.CENTER);
            barTextView.setTextColor(Color.BLACK);
            barTextView.setTextSize(DpUtil.dp2sp(context, 19));
            centerBody.addView(barTextView);
        }
        this.barText = barText;
        barTextView.setText(barText);

        if (centerBody != null && barSubTextView == null) {
            barSubTextView = getBarTextView();
            barSubTextView.setId(BARSUBTEXTID);
            barSubTextView.setGravity(Gravity.CENTER);
            barSubTextView.setTextColor(Color.BLACK);
            barSubTextView.setTextSize(DpUtil.dp2sp(context, 10));
            centerBody.addView(barSubTextView);
        }
        if (subBarText == null || subBarText.equals("")) {
            barSubTextView.setVisibility(GONE);
        } else {
            barSubTextView.setVisibility(VISIBLE);
        }
        this.subBarText = subBarText;
        barSubTextView.setText(subBarText);

        //防止第二次设置文字的时候 又走一次
        if (barTextView != null || barSubTextView != null) {
            setTextColorAuto(true);
        }
    }

    @Override
    public BarTextView getBarCenterText() {
        initBarText(barText, subBarText);
        return barTextView;
    }

    /**
     * @return
     * @Author:gewu
     * @Time:2022/10/2723:17
     * @Description:若隐藏副标题可直接设置为NULL
     */
    @Override
    public BarTextView getBarSubCenterText() {
        initBarText(barText, subBarText);
        return barSubTextView;
    }

    boolean textColorIsAuto = true;
    boolean imageColorIsAuto = true;

    @Override
    public void setTextColorAuto(boolean isAuto) {
        this.textColorIsAuto = isAuto;
        if (barTextView != null && isAuto) {
            barTextView.setTextColorWithBar(barColor);
        }

        if (barSubTextView != null && isAuto) {
            barSubTextView.setTextColorWithBar(barColor);
        }

        setBarLeftRightTextColor(leftBody, isAuto, barColor);
        setBarLeftRightTextColor(rightBody, isAuto, barColor);
    }

    @Override
    public void setImageColorAuto(boolean isAuto) {
        this.imageColorIsAuto = isAuto;
        setBarLeftRightImageColor(leftBody, isAuto, barColor);
        setBarLeftRightImageColor(rightBody, isAuto, barColor);
    }

    private void setBarLeftRightTextColor(LinearLayout view, boolean isAuto, int color) {
        if (view != null) {
            for (int i = 0; i < view.getChildCount(); i++) {
                if (view.getChildAt(i) instanceof BarTextView) {
                    if (isAuto) {
                        ((BarTextView) view.getChildAt(i)).setTextColorWithBar(color);
                    } else {
                        ((BarTextView) view.getChildAt(i)).setTextColor(color);
                    }
                }
            }
        }
    }

    private void setBarLeftRightImageColor(LinearLayout view, boolean isAuto, int color) {
        if (view != null) {
            for (int i = 0; i < view.getChildCount(); i++) {
                if (view.getChildAt(i) instanceof BarImageView) {
                    if (isAuto) {
                        ((BarImageView) view.getChildAt(i)).setImageColorWithBar(color);
                    } else {
                        ((BarImageView) view.getChildAt(i)).setColorFilter(color);
                    }
                }
            }
        }
    }

    @Override
    public void setBarTextColor(int color) {
        if (barTextView != null) barTextView.setTextColor(color);
        setBarLeftRightTextColor(leftBody, false, color);
        setBarLeftRightTextColor(rightBody, false, color);
    }

    @Override
    public void setBarImageColor(int color) {
        setBarLeftRightImageColor(leftBody, false, color);
        setBarLeftRightImageColor(rightBody, false, color);
    }

    @Override
    public void setBarSubTextColor(int color) {
        if (barSubTextView != null) barSubTextView.setTextColor(color);
    }


    private LinearLayout leftBody = null;
    private List<BarItem> leftMenus;

    @Override
    public void createLeftBody(BarMenu barMenu) {
        if (barMenu == null) return;
        if (leftBody == null) {
            leftBody = new LinearLayout(context);
            leftBody.setId(LEFTBODYID);
            leftBody.setOrientation(LinearLayout.HORIZONTAL);
            leftBody.setGravity(Gravity.CENTER_VERTICAL);
            leftBody.setPadding(leftBodyPadding, 0, leftBodyPadding, 0);
            LayoutParams leftBodyLp = getLayoutParams(WRAP_CONTENT, WRAP_CONTENT);
            leftBodyLp.addRule(CENTER_VERTICAL, barView.getId());
            leftBody.setLayoutParams(leftBodyLp);
            barView.addView(leftBody);
        }
        leftMenus = barMenu.getLeftMenus();
        createBody(leftBody, leftMenus, 19, 5);
        invalidate();
    }

    @Override
    public void setLeftBodyChildPadding(float padding) {
        createBody(leftBody, leftMenus, 19, (int) padding);
    }

    private LinearLayout rightBody;
    private List<BarItem> rightMenus;

    @Override
    public void createRightBody(BarMenu barMenu) {
        if (barMenu == null) return;
        if (rightBody == null) {
            int rightBodyTopPadding = DpUtil.dp2PxInt(context, 2);
            int rightBodyPadding = DpUtil.dp2PxInt(context, 10);
            rightBody = new LinearLayout(context);
            rightBody.setId(RIGHTBODYID);
            rightBody.setOrientation(LinearLayout.HORIZONTAL);
            rightBody.setGravity(Gravity.CENTER_VERTICAL);
            rightBody.setPadding(rightBodyPadding, rightBodyTopPadding, rightBodyPadding, 0);
            LayoutParams rightBodyParams = getLayoutParams(WRAP_CONTENT, WRAP_CONTENT);
            rightBodyParams.addRule(ALIGN_PARENT_RIGHT, barView.getId());
            rightBodyParams.addRule(CENTER_VERTICAL, barView.getId());
            rightBody.setLayoutParams(rightBodyParams);
            barView.addView(rightBody);
        }
        rightMenus = barMenu.getRightMenus();
        createBody(rightBody, rightMenus, 15, 5);
        invalidate();
    }

    @Override
    public void setRightBodyChildPadding(float rightBodyPadding) {
        createBody(rightBody, rightMenus, 15, (int) rightBodyPadding);
    }

    private void createBody(LinearLayout view, List<BarItem> menus, int textSize, int childPadding) {
        if (view.getChildCount()!=0 && menus.size()!=0){
            view.removeAllViews();
        }
        int padding = DpUtil.dp2PxInt(context, childPadding);
        for (BarItem item : menus) {
            switch (item.getBarType()) {
                case TEXT:
                    BarTextView barTextView = getBarTextView();
                    barTextView.setText(item.getText());
                    barTextView.setTextColorWithBar(barColor);
                    barTextView.setTextSize(DpUtil.dp2sp(context, textSize));
                    view.addView(barTextView);
                    item.setBarTextView(barTextView);
                    break;
                case ICON:
                    BarImageView barImageViewIcon = getBarImageView();
                    barImageViewIcon.setImageResource(item.getIcon());
                    barImageViewIcon.setColorFilter(Color.WHITE);
                    barImageViewIcon.setImageColorWithBar(barColor);
                    view.addView(barImageViewIcon);
                    item.setBarImageView(barImageViewIcon);
                    break;
                case BITMAP:
                    BarImageView barImageViewBitMap = getBarImageView();
                    barImageViewBitMap.setImageBitmap(item.getBitmap());
                    barImageViewBitMap.setImageColorWithBar(barColor);
                    view.addView(barImageViewBitMap);
                    item.setBarImageView(barImageViewBitMap);
                    break;
            }
            if (menus.indexOf(item) == 0) {
                setPadding(item, 0, 0);
            } else if (menus.indexOf(item) == menus.size() - 1) {
                setPadding(item, padding, 0);
            } else {
                setPadding(item, padding, padding);
            }
        }

    }

    private void setPadding(BarItem barItem, int leftPadding, int rightPadding) {
        BarType barType = barItem.getBarType();
        switch (barType) {
            case TEXT:
                barItem.getBarTextView().setPadding(leftPadding, 0, rightPadding, 0);
                break;
            case ICON:
            case BITMAP:
                barItem.getBarImageView().setPadding(leftPadding, 0, rightPadding, 0);
                break;
        }
    }

    private int count;

    @Override
    public BarTextView getBarWhichTextView(List<BarItem> menus, int which) {
        if (menus != null && menus.size() > 1) {
            for (BarItem item : menus) {
                if (item.getBarType() == BarType.TEXT) {
                    if (count == which) {
                        count = 0;
                        return item.getBarTextView();
                    } else {
                        count++;
                    }
                }
            }
        }
        return null;
    }

    @Override
    public BarImageView getBarWhichImageView(List<BarItem> menus, int which) {
        if (menus != null && menus.size() > 1) {
            for (BarItem item : menus) {
                if (item.getBarType() == BarType.ICON || item.getBarType() == BarType.BITMAP) {
                    if (count == which) {
                        count = 0;
                        return item.getBarImageView();
                    } else {
                        count++;
                    }
                }
            }
        }
        return null;
    }

    @Override
    public void setBackgroundColor(int color) {
        this.barColor = color;
        if (topView != null && barView != null) {
            topView.setBackgroundColor(color);
            barView.setBackgroundColor(color);
            if (!isInEditMode())
                TitleUtil.setStatusBarTransparent(context, color);
        }

        if (textColorIsAuto) {
            if (leftBody != null && leftBody.getChildCount() > 0) {
                setBarLeftRightTextColor(leftBody, textColorIsAuto, color);
            }
            if (rightBody != null && rightBody.getChildCount() > 0) {
                setBarLeftRightTextColor(rightBody, textColorIsAuto, color);
            }
        }

        if (imageColorIsAuto) {
            if (leftBody != null && leftBody.getChildCount() > 0) {
                setBarLeftRightImageColor(leftBody, imageColorIsAuto, color);
            }
            if (rightBody != null && rightBody.getChildCount() > 0) {
                setBarLeftRightImageColor(rightBody, imageColorIsAuto, color);
            }
        }

    }

    private BarTextView getBarTextView() {
        return new BarTextView(context);
    }

    private BarImageView getBarImageView() {
        return new BarImageView(context);
    }

    private LayoutParams getLayoutParams(int w, int h) {
        return new LayoutParams(w, h);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        View childAt = getChildAt(1);
        LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();

        int widthMeasureSpecs = 0;
        int heightMeasureSpecs = 0;

        switch (widthMode) {
            case MeasureSpec.AT_MOST:
                widthMeasureSpecs = measuredWidth + childAt.getPaddingRight() + childAt.getPaddingLeft() + layoutParams.leftMargin + layoutParams.rightMargin;
                break;
            case MeasureSpec.EXACTLY:
                widthMeasureSpecs = measuredWidth;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }

        switch (heightMode) {
            case MeasureSpec.AT_MOST:
                heightMeasureSpecs = measuredHeight + childAt.getPaddingTop() + childAt.getPaddingBottom() + layoutParams.topMargin + layoutParams.bottomMargin;
                break;
            case MeasureSpec.EXACTLY:
                heightMeasureSpecs = measuredHeight;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }

        setMeasuredDimension(widthMeasureSpecs, heightMeasureSpecs);
    }


    @Override
    public BarEditText createEditText() {
        BarEditText editText = new BarEditText(context);
        editText.setGravity(Gravity.CENTER_VERTICAL);
        barView.addView(editText);
        int height = DpUtil.dp2PxInt(context, 30);
        LayoutParams layoutParams = getLayoutParams(MATCH_PARENT, height);
        layoutParams.addRule(CENTER_IN_PARENT,barView.getId());
        if (centerBody!=null){
            barView.removeView(centerBody);
        }
        if (leftBody!=null){
            layoutParams.addRule(RIGHT_OF,leftBody.getId());
        }
        if (rightBody!=null){
            layoutParams.addRule(LEFT_OF,rightBody.getId());
        }
        editText.setLayoutParams(layoutParams);
        return editText;
    }
}
